package com.example.demo.testLearning.completableFuture;

import java.util.concurrent.CompletableFuture;

/**
 * @Author CHAS
 * 2024/8/23 0:02
 **/

public class CompletableFutureTest {
    public static void main(String[] args) {
        CompletableFuture<String> stringCompletableFuture = CompletableFuture.supplyAsync(() -> "Hello, World!");
        CompletableFuture<String> future = new CompletableFuture<>();
        boolean complete = future.complete("Hello, World!");
        CompletableFuture.supplyAsync(() -> "Hello")
                .thenApply(result -> result + ", World!")
                .thenAccept(System.out::println);

        CompletableFuture<String> future1 = CompletableFuture.supplyAsync(() -> {
            if (Math.random() > 0.5) {
                throw new RuntimeException("Something went wrong");
            }
            return "Success";
        });
        future1.handle((result, ex) -> {
            if (ex != null) {
                return "Exception: " + ex.getMessage();
            }
            return result;
        }).thenAccept(System.out::println);
        future1.exceptionally(ex -> "Exception: " + ex.getMessage())
                .thenAccept(System.out::println);
//         final ScheduledExecutorService executorService = Executors.newScheduledThreadPool(10); // 预设线程池大小
//        如果超过了线程池的并发数是会等待吗:会
//        CompletableFuture.get超时会抛TimeoutException
    }
}
